1 module std.stdio;
2 import core.stdc.stdio;
3 
4 version(WebAssembly)
5 {
6     import arsd.webassembly;
7     extern(C) void jsprint(uint length, const(char)* str) @nogc nothrow;
8     void writeln(Args...)(Args args) nothrow @nogc
9     {
10         static if(args.length == 1 && is(typeof(args[0]) == string))
11         {
12             jsprint(args[0].length, args[0].ptr);
13         }
14         else
15         {
16             // version(Have_util)
17             // {
18             //     import hip.util.string;
19             //     String s = String(args);
20             //     jsprint(s.length, s.chars.ptr);
21             // }
22             // else
23             // {
24                 eval(q{
25                 console.log.apply(null, arguments);
26                 }, args);
27             // }
28         }
29     }
30 }
31 version(PSVita)
32 {
33     extern(C) void hipVitaPrint(uint length, const(char)* str);
34     void writeln(Args...)(Args args)
35     {
36         import hip.util.string;
37         String str = String(args);
38         hipVitaPrint(str.length, cast(const(char)*)str.ptr);
39     }
40 }
41 version(CustomRuntimeTest)
42 {
43     void writeln(Args...)(Args args)
44     {
45         import hip.util.string;
46         String str = String(args);
47         printf("%.*s", str.length, cast(const(char)*)str.ptr);
48     }
49 }
50 
51 struct File
52 {
53     FILE* fptr;
54     private size_t _size;
55 
56     size_t size(){return _size;}
57 
58     this(string path, string openMode = "r")
59     {
60         fptr = fopen((path~'\0').ptr, (openMode~'\0').ptr);
61         if(fptr != null)
62         {
63             fseek(fptr, 0, SEEK_END);
64             auto tempSize = ftell(fptr);
65             if(tempSize > 0)
66                 _size = cast(size_t)tempSize;
67             fseek(fptr, 0, SEEK_SET);
68         }
69     }
70 
71     void rawWrite(string data){rawWrite(cast(ubyte[])data);}
72     void rawWrite(ubyte[] data)
73     {
74         if(fptr != null)
75         {
76             foreach(b; data)
77             {
78                 if(fputc(b, fptr) == EOF)
79                     break;
80             }
81         }
82     }
83 
84     void[] rawRead(void[] buffer){return cast(void[])rawRead(cast(ubyte[])buffer);}
85     ubyte[] rawRead(ubyte[] buffer)
86     {
87         if(fptr != null)
88         {
89             int ch = -1;
90             size_t totalRead = 0;
91             while((ch = fgetc(fptr)) != EOF && totalRead < buffer.length)
92                 buffer[totalRead++] = cast(ubyte)ch;
93             
94             return buffer[0..totalRead];
95         }
96         return buffer;
97     }
98 
99     void seek(ptrdiff_t offset, int origin = SEEK_SET)
100     {
101         if(fptr != null)
102         {
103             fseek(fptr, offset, origin);
104         }
105     }
106     void close()
107     {
108         if(fptr != null)
109         {
110             fclose(fptr);
111             fptr = null;
112         }
113     }
114 
115     ~this()
116     {
117         if(fptr != null)
118         {
119             fclose(fptr);
120             fptr = null;
121         }
122     }
123 }